<?php
/**
 * todu
 *
 * @copyright Copyright (c) 2018 Create-Chain PHP Group
 * @author    atangege
 */

namespace App\Exports;

use App\Model\Order;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class OrderExport implements FromQuery, WithHeadings, WithMapping, ShouldAutoSize
{
    private $condition;

    public function __construct($condition)
    {
        $this->condition = $condition;
    }

    public function map($order): array
    {
        $detail = json_decode($order->detail, true);

        return [
            $order->code,
            $order->payment_code,
            $detail[0]['specification'],
            $detail[0]['commodity_code'],
            $detail[0]['commodity_sku_code'],
            $detail[0]['amount'],
            sprintf('%.2f', $order->payment_amount / 100),
            $order->payment_platform ?? '在线付款',
            $order->province,
            $order->city,
            $order->county,
            $order->address,
            $order->contact_name,
            $order->contact_phone,
            $order->paid_at,
            Order::multiple($order->status),
        ];
    }

    public function query()
    {
        return Order::query()->whereIn('code', $this->condition);
    }

    public function headings(): array
    {
        return [
            ['订单号', '交易单号', '商品标题', '自定义sku', 'sku值', '购买数量', '实付金额', '付款方式', '省', '市', '区', '详细地址', '收件人姓名', '收件人电话', '支付时间', '订单状态']
        ];
    }

}
